import axios from 'axios'
import Vue from 'vue'

// 创建一个AXIOS实例
const service = axios.create({
  baseURL: process.env.VUE_APP_CHAT_API, // url = base url + request url
  withCredentials: true, // 跨域请求时发送cookies
  timeout: 50000 // 请求超时
})
// 请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }

)
// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code !== 0) {
      Vue.prototype.$message({
        message: res.message || res.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      if (response.status !== 200) {
        return Promise.reject(new Error(res.message || 'Error'))
      }
    }
    return res
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

// 查询聊天组
export function chatGroup(params) {
  return service({
    url: `/chat/group`,
    method: 'get',
    params
  })
}
// 查询聊天记录
export function chatRecord(params) {
  return service({
    url: `/chat/record`,
    method: 'get',
    params
  })
}
